<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="updateAnnouncement"){
        $announcement = $_POST['announcement'];
        $update = array(
            "is_active"=>0
            );
        $condition = array(
            "company_id"=>$company_id
            );
        $db->update("tbannouncement",$update,$condition);
        $insertAnnoun = array(
            "company_id"=>$company_id,
            "announcement"=>$announcement,
            "is_active"=>1,
            "date"=>""
        );
        $db->insert("tbannouncement",$insertAnnoun);
    }elseif($_POST['action']=="getApproverWorflow"){
        $value = $_POST['value'];
        if($value==1){
            ?>
            <option value="head" class="pos_group">Head</option>
            <option value="asst_head" class="pos_group">Assistant Head</option>
            <?php
        }elseif($value==2){
            $getPosition = $db->query("SELECT * FROM tbposition WHERE company_id='$company_id' and is_active=1","array");
            foreach($getPosition as $setPosition){
                ?>
                <option class="pos_company"><?php echo $setPosition['position']; ?></option>
                <?php
            }
        }else if($value==3){
            $queryUsers = $db->query("SELECT * from tbuser where status = 1 and company_id = '$company_id' and is_active = 1","array");
            foreach($queryUsers as $dataUsers){
                ?>
                <option class="specific_name"><?php echo setName($dataUsers['firstname'],"",$dataUsers['lastname']); ?></option>
                <?php
            }
        }
    }elseif($_POST['action']=="getStatusWorflow"){
        ?>
        <option value="review" class="workFlowStat">For Review</option>
        <option value="approval" class="workFlowStat">For Approval</option>
        <?php
    }else if($_POST['action']=="addPosition"){
        $position = $_POST['position'];
        $getMatchPosition = $db->query("SELECT * FROM tbposition WHERE position = '$position' and company_id='$company_id' and is_active=1","numrows");
        if($getMatchPosition>=0){
            $insertAnnoun = array(
                "company_id"=>$company_id,
                "position"=>$_POST['position'],
                "is_active"=>1
            );
            $db->insert("tbposition",$insertAnnoun);
            echo 1;    
        }else{
            echo 0;  
        }
    }else if($_POST['action']=="getApproverTypeAdminSett"){
        $value = $_POST['value'];
        $type = $_POST['type'];
        if($value==1){
            //Department , Role Type
            $getActiveDept = $db->query("SELECT * FROM tborgchart o LEFT JOIN tborgchartobjects oo on o.id=oo.orgChart_id where o.status = 1 and o.is_active = 1 and o.company_id ='$company_id'","array");
            ?>
            <label>Department:</label>
            <select class="approverTypeAdminSett<?php echo $type; ?> <?php echo $type; ?>" rel="role">
            <?php
            foreach($getActiveDept as $setActiveDept){
                ?>
                <option value="<?php echo $setActiveDept['role']; ?>"><?php echo $setActiveDept['role']; ?></option>
                <?php
            }
            ?>
            </select>
            <label>Role Type:</label>
            <select class="roleTypeAdminSett<?php echo $type; ?> <?php echo $type; ?>" rel="role">
                <option value="1" class="pos_group">Head</option>
                <option value="2" class="pos_group">Assistant Head</option>
            </select>
            <?php
        }else if($value==2){
            $queryUsers = $db->query("SELECT * from tbuser where status = 1 and company_id = '$company_id' and is_active = 1","array");
            ?>
            <label>Specific User:</label>
            <select class="specific_name <?php echo $type; ?>" rel="specificUser">
            <?php
            foreach($queryUsers as $dataUsers){
                ?>
                <option value="<?php echo setName($dataUsers['firstname'],"",$dataUsers['lastname']); ?>"><?php echo setName($dataUsers['firstname'],"",$dataUsers['lastname']); ?></option>
                <?php
            }
            ?>
            </select>
            <?php
        }
    }else if($_POST['action']=="moveRequest"){
        $errorUser = 0;
        $errorPendingReq = 0;
        $pendingRequest_id = array();
        //get
        if($_POST['getUserType']==1){
            //echo "Per Department";
            $getUsersArray = explode("_",$_POST['getUser']);
            $getUserDb = $db->query("SELECT * FROM tbuser where role_type='".$getUsersArray[0]."' and role='".$getUsersArray[1]."' and company_id='$company_id'","row");
            $getUsersPerName =  $getUserDb['firstname']." ".$getUserDb['lastname'];
            $getUsersPerDept =  tempGroupType($getUsersArray[0])."_".$getUsersArray[1];
            $getUserRole = $getUsersArray[1];
            $getUserRoleType = $getUsersArray[0];
            $getUser_id = $getUserDb['id'];
        }else if($_POST['getUserType']==2){
            //echo "Per User";
            $getUserDb = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '".$_POST['getUser']."' and company_id='$company_id'","row");
            $getUsersPerName = $_POST['getUser'];
            $getUserRole = $getUserDb['role'];
            $getUserRoleType = $getUserDb['role_type'];
            $getUsersPerDept = tempGroupType($getUserRoleType)."_".$getUserRole;
            $getUser_id = $getUserDb['id'];
        }
        //echo $getUsersPerName." or ".$getUsersPerDept;
        $getUsersPerDeptArray = explode("_",$getUsersPerDept);
        $getUserRows = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '$getUsersPerName' or (role_type='".$getUsersPerDeptArray[0]."' and role='".$getUsersPerDeptArray[1]."') and status = 1 and company_id='$company_id' and is_active = 1","numrows");
        if($getUserRows>0){
            $getPendingRequestRows = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rr on r.id=rr.request_id where r.company_id = '$company_id' and rr.status = 0 and (rr.approver='$getUsersPerName' or rr.approver='$getUsersPerDept')","numrows");
            $getPendingRequest = $db->query("SELECT rr.request_id as request_id,rr.id as rr_id FROM tbrequest r LEFT JOIN tbrequest_process rr on r.id=rr.request_id where r.company_id = '$company_id' and rr.status = 0 and (rr.approver='$getUsersPerName' or rr.approver='$getUsersPerDept')","array");
            if($getPendingRequestRows>0){
                foreach($getPendingRequest as $dataPendingRequest){
                    //echo "Request_id: ".$dataPendingRequest['request_id']." Request process_id: ".$dataPendingRequest['rr_id']."\n";
                    array_push($pendingRequest_id,$dataPendingRequest['rr_id']);
                }
            }else{
                //echo "No Pending Request";
                $errorPendingReq++;
            }
        }else{
            //echo "No User in the position";
            $errorUser++;
        }
        
        if($_POST['setUserType']==1){
            //echo "Per Department";
            $setUsersArray = explode("_",$_POST['setUser']);
            $setUserDb = $db->query("SELECT * FROM tbuser where role_type='".$setUsersArray[0]."' and role='".$setUsersArray[1]."' and company_id='$company_id'","row");
            $setUsersPerName =  $setUserDb['firstname']." ".$setUserDb['lastname'];
            $setUsersPerDept =  tempGroupType($setUsersArray[0])."_".$setUsersArray[1];
            $setUserRole = $setUsersArray[1];
            $setUserRoleType = $setUsersArray[0];
            $setUser_id = $setUserDb['id'];
        }else if($_POST['setUserType']==2){
            //echo "Per User";
            $setUserDb = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '".$_POST['setUser']."' and company_id='$company_id'","row");
            $setUsersPerName = $_POST['setUser'];
            $setUserRole = $setUserDb['role'];
            $setUserRoleType = $setUserDb['role_type'];
            $setUsersPerDept = tempGroupType($setUserRoleType)."_".$setUserRole;
            $setUser_id = $setUserDb['id'];
        }
        //echo $getUsersPerName." or ".$getUsersPerDept;
        $setUsersPerDeptArray = explode("_",$setUsersPerDept);
        $setUserRows = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '$setUsersPerName' or (role_type='".$setUsersPerDeptArray[0]."' and role='".$setUsersPerDeptArray[1]."') and status = 1 and company_id='$company_id' and is_active = 1","numrows");
        if($setUserRows>0){
            //echo $setUserRows;
        }else{
            //echo "No User in the position";
            $errorUser++;
        }
        
        /**************/
        if($errorUser>0){
            echo "Error User";
        }else{
            if($getUser_id!=$setUser_id){
                $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$getUserRole' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $errorChild = 0;
                $d = 0;
                while(true){
                    //FOR WORKFLOW ROLE
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tborgchartline where master_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tborgchartline where master_id = '".$master['child_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                    }
                    $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$master['child_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                    if($master['child_id']==""){
                        break;
                    }else{
                        if($queryMasterNodeOrgChart['role']==$setUserRole){
                            $errorChild++;
                            break;   
                        }
                    }
                    $d++;
                }
                if($getUserRole==$setUserRole && $getUserRoleType==1 && $setUserRoleType = 2){
                    $errorChild++;
                }
                if($errorChild>0){
                    echo "Mas mababa ung role";
                }else{
                    if($errorPendingReq>0){
                        echo "Error Pending";
                    }else{
                        for($a=0;$a<count($pendingRequest_id);$a++){
                            $update = array(
                                "approver"=>setApprover($_POST['setUser'])
                                );
                            $where = array(
                                "id"=>$pendingRequest_id[$a]
                                );
                            $db->update("tbrequest_process",$update,$where);
                        }
                        echo 1;
                    }
                }
            }else{
                echo "Same User not Allowed";
            }
        }
    }
}
function tempGroupType($groupType){
    if($groupType=="1"){
        return "Head";
    }else if($groupType=="2"){
        return "Assistant Head";
    }
}
function setApprover($approver){
    if(strpos($approver,"_")==true){
        $setApprover = explode("_",$approver);
        return tempGroupType($setApprover[0])."_".$setApprover[1];
    }else{
        return $approver;
    }
}
?>